home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / listfile.c < prev    next >
C/C++ Source or Header  |  1995-04-10  |  3KB  |  112 lines

  1. /* Major/minor version numbers: by Glenn Ammons <ammons@access.digex.net>
  2.    thanks for the contrib..
  3. */   
  4.  
  5. #include "kiss.h"
  6.  
  7. #define DEV_MINOR(x) (x & 0xff)
  8. #define DEV_MAJOR(x) ((x & 0xff00) >> 8)
  9.  
  10. int listfile (char *name, LsFlags fl)
  11. {
  12.     struct stat
  13.     statbuf;
  14.     char
  15.     buf [FILENAMELEN] = { '\0' },
  16.     tmp [FILENAMELEN];
  17.     struct passwd
  18.     *pwd;
  19.     struct group
  20.     *grp;
  21.     struct tm
  22.     *loctime;
  23.  
  24.     if (lstat (name, &statbuf) && stat (name, &statbuf))
  25.     return (warning ("can't stat \"%s\"", name));
  26.  
  27.     if (fl.longoutput)
  28.     {
  29.     sprintf (buf, "%c%c%c%c%c%c%c%c%c%c %2d",
  30.          S_ISLNK (statbuf.st_mode) ? 'l' 
  31.          : S_ISREG (statbuf.st_mode) ? '-'
  32.          : S_ISDIR (statbuf.st_mode) ? 'd'
  33.          : S_ISCHR (statbuf.st_mode) ? 'c'
  34.          : S_ISBLK (statbuf.st_mode) ? 'b'
  35.          : S_ISFIFO (statbuf.st_mode) ? 'p'
  36.          : S_ISSOCK (statbuf.st_mode) ? 's'
  37.          : '?',
  38.          S_IRUSR & statbuf.st_mode ? 'r' : '-',
  39.          S_IWUSR & statbuf.st_mode ? 'w' : '-',
  40.          (S_IXUSR & statbuf.st_mode
  41.                   && S_ISUID & statbuf.st_mode) ? 's' 
  42.          : (S_IXUSR & statbuf.st_mode
  43.             && S_ISUID ^ statbuf.st_mode) ? 'x'
  44.          : (S_IXUSR ^ statbuf.st_mode
  45.             && S_ISUID & statbuf.st_mode) ? 'S' : '-',
  46.          S_IRGRP & statbuf.st_mode ? 'r' : '-',
  47.          S_IWGRP & statbuf.st_mode ? 'w' : '-',
  48.          (S_IXGRP & statbuf.st_mode
  49.                   && S_ISGID & statbuf.st_mode) ? 's' 
  50.          : (S_IXGRP & statbuf.st_mode
  51.             && S_ISGID ^ statbuf.st_mode) ? 'x'
  52.          : (S_IXGRP ^ statbuf.st_mode
  53.             && S_ISGID & statbuf.st_mode) ? 'S' : '-',
  54.          S_IROTH & statbuf.st_mode ? 'r' : '-',
  55.          S_IWOTH & statbuf.st_mode ? 'w' : '-',
  56.          (S_IXOTH & statbuf.st_mode
  57.                   && S_ISVTX & statbuf.st_mode) ? 't' 
  58.          : (S_IXOTH & statbuf.st_mode
  59.             && S_ISVTX ^ statbuf.st_mode) ? 'x'
  60.          : (S_IXOTH ^ statbuf.st_mode
  61.             && S_ISVTX & statbuf.st_mode) ? 'T' : '-',
  62.          (int) statbuf.st_nlink);
  63.  
  64.     if ( (pwd = getpwuid (statbuf.st_uid)) )
  65.         sprintf (tmp, " %9s", pwd->pw_name);
  66.     else
  67.         sprintf (tmp, " %9d", statbuf.st_uid);
  68.     strcat (buf, tmp);
  69.  
  70.     if ( (grp = getgrgid (statbuf.st_gid)) )
  71.         sprintf (tmp, " %9s", grp->gr_name);
  72.     else
  73.         sprintf (tmp, " %9d",statbuf.st_gid);
  74.     strcat (buf, tmp);
  75.  
  76.     if (S_ISCHR (statbuf.st_mode) 
  77.         || S_ISBLK (statbuf.st_mode))
  78.       sprintf (tmp, " %4ld,%4ld", 
  79.            (long) DEV_MAJOR(statbuf.st_rdev),
  80.            (long) DEV_MINOR(statbuf.st_rdev));
  81.     else
  82.       sprintf (tmp, " %9ld", (long) statbuf.st_size);
  83.     strcat (buf, tmp);
  84.  
  85.     loctime = localtime (&statbuf.st_mtime);
  86.     sprintf (tmp, " %2.2d/%2.2d/%2.2d %2.2d:%2.2d ",
  87.          loctime->tm_mon, loctime->tm_mday, loctime->tm_year,
  88.          loctime->tm_hour, loctime->tm_min);
  89.     
  90.     strcat (buf, tmp);
  91.     }
  92.  
  93.     strcat (buf, name);
  94.  
  95.     if (fl.listtype)
  96.     {
  97.     if (S_ISDIR (statbuf.st_mode))
  98.         strcat (buf, "/");
  99.     else if (S_ISFIFO (statbuf.st_mode))
  100.         strcat (buf, "=");
  101.     else if (S_ISLNK (statbuf.st_mode))
  102.         strcat (buf, "@");
  103.     }
  104.  
  105.     if (fl.oneperline)
  106.     puts (buf);
  107.     else
  108.     listoutput (buf);
  109.  
  110.     return (0);
  111. }
  112.